package com.keshi.openplatform.monitor.config;


import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.keshi.openplatform.monitor.job.AvgJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MonitorConfig {


    /**
     * 用作分布式锁的注册中心
     * @param serverList
     * @param nameSpace
     * @return
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter zookeeperRegistryCenter(@Value(("${elasticjob.regcenter.serverList}")) String serverList,@Value(("${elasticjob.regcenter.namespace}")) String nameSpace) {
        ZookeeperConfiguration configuration=new ZookeeperConfiguration(serverList,nameSpace);
        configuration.setConnectionTimeoutMilliseconds(10000);
        configuration.setMaxRetries(5);
        ZookeeperRegistryCenter zookeeperRegistryCenter=new ZookeeperRegistryCenter(configuration);
        return zookeeperRegistryCenter;
    }

    /**
     * 配置任务的触发器
     * @param corn
     * @param shardingTotalCount
     * @param shardingparamters
     * @return
     */

    @Bean
    public LiteJobConfiguration liteJobConfiguration(@Value(("${elasticjob.corn}")) String corn,@Value(("${elasticjob.count}")) int shardingTotalCount,@Value(("${elasticjob.shardingparamters}")) String shardingparamters) {

        JobCoreConfiguration coreConfiguration= JobCoreConfiguration.newBuilder(AvgJob.class.getName(),corn,shardingTotalCount).shardingItemParameters(shardingparamters).build();
        LiteJobConfiguration configuration = LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(coreConfiguration, AvgJob.class.getCanonicalName())).overwrite(true).build();
        return configuration;
    }

    @Bean(initMethod = "init")
    public SpringJobScheduler springJobScheduler(AvgJob job, ZookeeperRegistryCenter registryCenter, LiteJobConfiguration configuration) {
        return new SpringJobScheduler(job, registryCenter, configuration);
    }
}
